var device=request("from")
var phone=request("phone")
var city=request("city")
var from_type=request("type")

$(function(){
	//添加驾校
	$("#add_school").click(function(){
		$(".add_mask").fadeIn()
		$(".add_content").animate({
			"bottom":"0",
		},300)
	})
	$(".add_content .cancel").click(function(){
		$(".add_mask").fadeOut()
		$(".add_content").animate({
			"bottom":"-130px",
		},300)
	})
	$(".add_content .enter").click(function(){
		if($("#add_school_name").val().trim()==""){
			$("#prompt").text("请输入驾校名称");
            $(".prompt").fadeIn(300,function(){
                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
            })
            return false;
		}
		if(from_type=="1"){
			location.href="qicheng://select_school?city="+city+"&school="+$("#add_school_name").val().trim()
		}
		else{
			//添加驾校
			$.ajax({
			    url: DATA_URL+"addschool",
			    async: false,
			    data: "phone="+phone+"&area="+city+"&school="+$("#add_school_name").val().trim(),
			    type: "post",
			    dataType: 'json',
			    timeout: 5000,
			    success:function (data) {
			    	if(data.code=="0"){
			    		
			    	}
					else{
						$("#prompt").text(data.msg);
			            $(".prompt").fadeIn(300,function(){
			                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
			            })
					}
			    },
			    error:function () {
			    	$("#prompt").text("您的网络不给力哦~");
			        $(".prompt").fadeIn(300,function(){
			            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
			        })
			    }
			});
			
			set_school(phone,city,$("#add_school_name").val().trim())
		}
	})
	//获取驾校列表
	$.ajax({
	    url: DATA_URL+"schoollist",
	    data: "area="+city,
	    type: "post",
	    dataType: 'json',
	    timeout: 5000,
	    success:function (data) {
	    	if(data.code=="0"){
	    		var school_list=""
	    		$.each(data.info,function(index,val){
	    			school_list+="<div class='sort_list' city='"+val.area+"'>\
									<div class='num_name'>"+val.school+"</div>\
								</div>"	
	    		})
	    		$(".sort_box").html(school_list)
	    		initials()//字母排序
	    	}
			else{
				$("#prompt").text(data.msg);
	            $(".prompt").fadeIn(300,function(){
	                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
	            })
			}
	    },
	    error:function () {
	    	$("#prompt").text("您的网络不给力哦~");
	        $(".prompt").fadeIn(300,function(){
	            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
	        })
	    }
	});
	//选择驾校
	$(".sort_box").on("click",".sort_list",function(){
		if(from_type=="1"){
			location.href="qicheng://select_school?city="+city+"&school="+$(this).find(".num_name").text()
		}
		else{
			set_school(phone,$(this).attr("city"),$(this).find(".num_name").text())
		}
	})
	function set_school(phone_param,area_param,school_param){
		$.ajax({
		    url: DATA_URL+"setcouchschool",
		    data: "phone="+phone_param+"&area="+area_param+"&school="+school_param,
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
		    		if(device=="app"){
						location.href="qicheng://go_back"
					}
					else{
						location.replace("info_eidt.html?phone="+phone+"&from="+device)
					}
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
	}
})
